package com.systop.ltbook.mainpage;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.systop.cms.webview.service.ArticleFreeMarkerManager;
import com.systop.core.webapp.struts2.action.BaseAction;
import com.systop.ltbook.BookConstants;
import com.systop.ltbook.book.service.BookManager;

/**
 * 图书联盟首页Action类，主要功能是初始化首页图书、公告及资讯内容
 * 
 * @author DU
 */
@SuppressWarnings({ "serial", "rawtypes"})
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class MainPageAction extends BaseAction {

	/** 新书 */
	private static List<Map> newBooks = null;

	/** 热销 */
	private static List<Map> hotBooks = null;

	/** 特价 */
	private static List<Map> cheepBooks = null;
	
	// 推荐内容 
	private Map eliteInfo = null;
	
	// 推荐文章
	private List eliteArticles = null;
	
	// 公告文章
	private List annArticles = null;

	/** 图书管理类 */
	@Autowired
	private BookManager bookManager;
	
	@Autowired
	private ArticleFreeMarkerManager articleFreeMarkerManager;

	/**
	 * 用于初始化首页图书、公告及资讯内容
	 */
	public String index() {
		if (newBooks == null) {
			newBooks = bookManager.getBooksByType(BookConstants.TYPE_NEW,
					BookConstants.NWE_BOOK_PAGE_ZISE);
		}
		if (hotBooks == null) {
			hotBooks = bookManager.getBooksByType(BookConstants.TYPE_HOT,
					BookConstants.HOT_BOOK_PAGE_ZISE);
		}
		if (cheepBooks == null) {
			cheepBooks = bookManager.getBooksByType(BookConstants.TYPE_CHEEP,
					BookConstants.CHEEP_BOOK_PAGE_ZISE);
		}
		
		eliteInfo = articleFreeMarkerManager.getEliteArticlesInfo(null, 6, getRequest().getContextPath());
		eliteArticles = articleFreeMarkerManager.getEliteArticles(null, 6);
		annArticles = articleFreeMarkerManager.getArtsByCatName("公告", 10);
		return SUCCESS;
	}

	/**
	 * 用于外部修改新书，与首页数据同步
	 */
	public static void resetNewBooks() {
		newBooks = null;
	}

	/**
	 * 用于外部修改热销图书，与首页数据同步
	 */
	public static void resetHotBooks() {
		hotBooks = null;
	}

	/**
	 * 用于外部修改特价图书，与首页数据同步
	 */
	public static void resetCheepBooks() {
		cheepBooks = null;
	}

	public static List<Map> getNewBooks() {
		return newBooks;
	}

	public static List<Map> getHotBooks() {
		return hotBooks;
	}

	public static List<Map> getCheepBooks() {
		return cheepBooks;
	}

	public Map getEliteInfo() {
		return eliteInfo;
	}

	public List getEliteArticles() {
		return eliteArticles;
	}

	public List getAnnArticles() {
		return annArticles;
	}

}
